package mpc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import Sorters.ISorter;

public class ResultDrawer extends JPanel {
	
	private int width = 100, postAndSpaceWidth, spaceWidth;
	private ISorter ownSort;
	private Sortable[] ownWorkMas;
	private boolean finished = false;
	
	ResultDrawer(ISorter sorter, Sortable[] mas){
		attachSorter(sorter);
		attachMas(mas);
		this.setPreferredSize(new Dimension(width, width));
	}
	public void attachSorter(ISorter sorter){
		this.ownSort = sorter;
	}
	
	public void attachMas(Sortable[] mas){
		finished = false;
		ownWorkMas = new Sortable[mas.length];
		for(int i = 0; i < ownWorkMas.length; i++)
			ownWorkMas[i] = new Sortable(mas[i]);
		
		ownSort.attachMas(ownWorkMas);
		width = mas.length;
		this.repaint();
	}
	
	public boolean doNextIteration(){
		finished = ownSort.doNextIteration();
		return finished;
	}
	
	@Override
	public void paint(Graphics g){
		g.setColor(finished ? Color.getHSBColor(0.2f, 0.6f, 0.9f) : Color.getHSBColor(0.57f, 0.6f, 0.9f));
		g.fillRect(0, 0, width, width);
		g.setColor(Color.black);
		
		for(int i = 0; i < width; i++){
			g.drawRect(i, width - ownWorkMas[i].key, 1, ownWorkMas[i].key);
		}
	}
}
